package com.ruixin.config;

import com.ruixin.base.exception.BaseException;
import com.ruixin.mvc.config.ApplicationContextWrapper;

/**
 * Created Ruixin
 * Description:SpringApplication启动类
 */
public class SpringApplication {

    private ApplicationContextWrapper applicationContext;

    /**
     * 获取Bean
     * @param beanName
     */
    public Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    /**
     * 获取bean
     * @param clz
     * @return
     * @throws BaseException
     */
    public <T> T getBean(Class<T> clz) throws BaseException {
        return applicationContext.getBean(clz);
    }

    /**
     * 获取Context
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过加载类加载
     * @param clz
     * @return
     * @throws BaseException
     */
    public static SpringApplication builder(Class<?> clz,Object ... args) throws BaseException {
        SpringApplication springApplication = new SpringApplication();
        springApplication.applicationContext = ApplicationContextWrapper.builder(clz);
        return springApplication;
    }

    /**
     * 通过配置文件加载
     * @param configFile
     * @return
     * @throws BaseException
     */
    public static SpringApplication builder(String configFile) throws BaseException {
        SpringApplication springApplication = new SpringApplication();
        springApplication.applicationContext = ApplicationContextWrapper.builder(configFile);
        return springApplication;
    }

}
